package net.dgg.dggcloud.service.jrcrm.service.orf;

import net.dgg.dggcloud.core.json.JsonUtil;
import net.dgg.dggcloud.core.util.DateUtil;
import net.dgg.dggcloud.service.common.base.service.BaseService;
import net.dgg.dggcloud.service.common.exception.BizException;
import net.dgg.dggcloud.service.jrcrm.dao.orf.OrderCustomerDao;
import net.dgg.dggcloud.service.jrcrm.entity.orf.OrderCustomer;
import net.dgg.dggcloud.service.entity.user.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 鲜林材
 * 订单客户信息
 * @create 2017-11-06 16:35
 **/
@Service
public class OrderCustomerService extends BaseService {

    @Autowired
    private OrderCustomerDao orderCustomerDao;


    /**
     * 根据ID，获取订单相关信息
     *
     * @param id
     * @return
     */
    public OrderCustomer getByIdHiber(Long id) {
        return this.repository.get(OrderCustomer.class, id);
    }


    /**
     * 保存订单客户信息
     *
     * @param orderCustomer
     * @param user
     */
    @Transactional
    public void saveOrderCustomer(OrderCustomer orderCustomer, User user) {
        try {
            orderCustomer.setCreateUser(user);
            orderCustomer.setId(this.keyWorker.nextId());
            this.repository.save(orderCustomer);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("保存订单客户信息异常"+e.getMessage(),10006);
        }
    }

    /**
     * 查询订单联系信息通过map
     *
     * @param map 查询条件 <br/>
     *            orderId 订单id <br/>
     * @return
     */
    public List<Map> queryOrderCustomerListByMap(Map map) {
        List<Map> maps = orderCustomerDao.queryOrderCustomerListByMap(map);
        return maps;
    }

    /**
     * 通过订单id查询订单客户关联信息
     *
     * @param orderId
     * @return
     */
    public Map queryOrderCusttomerByOrderId(Long orderId) {
        Map map = new HashMap();
        OrderCustomer orderCustomer = this.repository.findUniqueByProperty(OrderCustomer.class, "orderId", orderId);
        if(orderCustomer!=null){
            map.put("customerDataInfo", transStringToJson(orderCustomer.getCustomerDataInfo()));
            map.put("subjectJson", transStringToJson(orderCustomer.getSubjectJson()));
            map.put("customerPropertyJson", transStringToJson(orderCustomer.getCustomerPropertyJson()));
            map.put("id",orderCustomer.getId());
        }
        return map;
    }

    /**
     * 把json字符串信息转换json对象
     *
     * @param val
     * @return
     */
    public Object transStringToJson(String val) {
        if (StringUtils.isNotEmpty(val)) {
            return JsonUtil.json2Obj(val, Object.class);
        }
        return null;
    }

    /**
     * 更新或者修改订单客户信
     *
     * @param orderCustomer
     * @param user
     */
    @Transactional
    public void saveOrUpdateOrderCustomer(OrderCustomer orderCustomer, User user) {
        try {
            if (orderCustomer.getId() != null) {
                OrderCustomer oldOrderCustomer = getByIdHiber(orderCustomer.getId());
                if(oldOrderCustomer!=null){
                    oldOrderCustomer.setSubjectJson(orderCustomer.getSubjectJson());
                    oldOrderCustomer.setCustomerDataInfo(orderCustomer.getCustomerDataInfo());
                    oldOrderCustomer.setCustomerPropertyJson(orderCustomer.getCustomerPropertyJson());
                }
                oldOrderCustomer.setUpdaterUser(user);
                this.repository.saveOrUpdate(oldOrderCustomer);
            } else {
                orderCustomer.setCreateUser(user);
                orderCustomer.setId(this.keyWorker.nextId());
                this.repository.saveOrUpdate(orderCustomer);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("更新订单客户信息异常："+e.getMessage(),10006);
        }
    }


    /**
     * 修改订单客户信息通过id,id必传
     * @param user
     * @param customerMap
     */
    @Transactional
    public void updateOrderCustomerByMap(User user,Map customerMap){
        Object id = customerMap.get("id");
        if(id!=null){
            customerMap.put("updateTime", DateUtil.dateToString(new Date()));
            customerMap.put("updaterId",user.getId());
            customerMap.put("updaterName",user.getName() + user.getLoginName());
            customerMap.put("updaterOrgId",user.getOrgId());
            orderCustomerDao.updateOrderCustomerByMap(customerMap);
        }else{
            throw new BizException("订单客户信息id不存在！",10006);
        }

    }

    /**
     * 通过订单id删除订单业务信息
     * @param parameterMap
     * 传入orderId
     */
    @Transactional
    public void deleteOrderCustomerByMap(Map parameterMap) {
        orderCustomerDao.deleteOrderCustomerByMap(parameterMap);
    }
}
